package cn.edu.nuc.zyb.frame;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import cn.edu.nuc.zyb.client.UserClient;
import cn.edu.nuc.zyb.model.Cart;
import cn.edu.nuc.zyb.model.Goods;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;
import java.awt.event.ActionEvent;

public class UserStore extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3914093898739832788L;
	private JPanel contentPane;
	private JTextField goodId;
	public JLabel labelcart;

	

	/**
	 * Create the frame.
	 */
	public UserStore(JFrame frame, String name) {// name是用户名

		setTitle("中北在线商场--当前用户:" + name);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));

		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		// model.addColumn("操作");
		
		//在构造方法里直接遍历
		UserClient uc = new UserClient();
		uc.listGoods();
		List<Goods> list = uc.acceptListGoods();

		for (int i = 0; i < list.size(); i++) {
			Goods goods = list.get(i);
			String id = String.valueOf(goods.getGid());
			String price = String.valueOf(goods.getPrice());
			String num = String.valueOf(goods.getNum());
			model.addRow(new String[] { id, goods.getGoodsName(), price, num });
		}

		JTable table = new JTable(model);
		
		//列表的单击事件
		/*table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// System.out.println("－－－－－＞" + p.toString());
				if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 1)) {
					int row = table.getSelectedRow();
					if (row == -1) {
						JOptionPane.showMessageDialog(UserStore.this, "请选择要查看的行");
					} else {
						String goodname = (String) table.getValueAt(row, 1);
						UserClient uc = new UserClient();
						uc.lookGoods(goodname);// 用商品和服务器交互
						Goods goods = uc.acceptLookGoods();
						UserGoods d = new UserGoods(goods, name);
						d.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
						d.setVisible(true);
					}
				}
			}
		});*/

		JScrollPane pane = new JScrollPane(table);

		panel.add(pane);
		contentPane.add(panel);
		labelcart = new JLabel("购物车:");
		labelcart.setBounds(10, 10, 124, 15);
		contentPane.add(labelcart);

		JButton button_2 = new JButton("查看商品详细信息(或双单击商品列)");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if (row == -1) {
					JOptionPane.showMessageDialog(UserStore.this, "请选择要查看的行");
				} else {
					String goodname = (String) table.getValueAt(row, 1);
					UserClient uc = new UserClient();
					uc.lookGoods(goodname);// 用商品和服务器交互
					Goods goods = uc.acceptLookGoods();
					UserGoods d = new UserGoods(goods, name);
					d.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					d.setVisible(true);
				}
			}
		});
		button_2.setBounds(407, 45, 299, 23);
		contentPane.add(button_2);

		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				UserClient uc = new UserClient();
				uc.exitLogin(name);
				frame.setVisible(true);
				UserStore.this.setVisible(false);

			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);

		JLabel lblid = new JLabel("商品名称:");
		lblid.setBounds(10, 53, 65, 15);
		contentPane.add(lblid);

		goodId = new JTextField();
		goodId.setBounds(85, 46, 104, 21);
		contentPane.add(goodId);
		goodId.setColumns(10);

		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				//搜索的所有事件
				if ("".equals(goodId.getText())) {
					model.setNumRows(0);
					UserClient uc = new UserClient();
					uc.changeNum(name);
					List<Goods> list = uc.acceptChangeNum();
					for (int i = 0; i < list.size(); i++) {
						Goods goods = list.get(i);
						String gid = String.valueOf(goods.getGid());
						String price = String.valueOf(goods.getPrice());
						String num = String.valueOf(goods.getNum());
						System.out.println(gid + "," + price + "," + num);
						model.addRow(new String[] { gid, goods.getGoodsName(), price, num });
					}
				} else {
					model.setNumRows(0);
					String goodsName = goodId.getText();
					UserClient uc = new UserClient();
					uc.searchGoods(goodsName);
					// String str = uc.acceptSearchGoods();
					Goods good = uc.acceptSearchGoods();
					try {
						String gid = String.valueOf(good.getGid());
						String price = String.valueOf(good.getPrice());
						String num = String.valueOf(good.getNum());
						System.out.println(gid + "," + price + "," + num);
						// String[] msgs=str.split(",");
						// model.addRow(new
						// String[]{msgs[0],msgs[1],msgs[2],msgs[3]});
						model.addRow(new String[] { gid, good.getGoodsName(), price, num });
					} catch (Exception e2) {
						System.out.println("关键字有问题");
					}

				}
			}
		});
		button_4.setBounds(203, 45, 93, 23);
		contentPane.add(button_4);

		JButton button_5 = new JButton("查看购物车");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				UserClient uc = new UserClient();
				uc.listCart(name);
				List<Cart> list = uc.acceptAddCart();
				UserCartFrame cf = new UserCartFrame(list, name);
				cf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				cf.setVisible(true);

			}
		});
		button_5.setBounds(116, 6, 110, 23);
		contentPane.add(button_5);

		JButton btnNewButton = new JButton("刷新");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UserClient uc = new UserClient();
				uc.listGoods();
				List<Goods> list = uc.acceptListGoods();
				model.setNumRows(0);
				for (int i = 0; i < list.size(); i++) {
					Goods goods = list.get(i);
					String id = String.valueOf(goods.getGid());
					String price = String.valueOf(goods.getPrice());
					String num = String.valueOf(goods.getNum());
					model.addRow(new String[] { id, goods.getGoodsName(), price, num });
				}

			}
		});
		btnNewButton.setBounds(310, 45, 83, 23);
		contentPane.add(btnNewButton);

		//这个线程就是为了实现实时购物车商品件数更新
		new Thread(new Runnable() {

			@Override
			public void run() {
				while (true) {
					File file = new File("D:\\store\\cart\\" + name);
					if (file.exists()) {
						File[] files = file.listFiles();
						labelcart.setText("购物车：" + files.length);
					} else {
						labelcart.setText("购物车：0");
					}
				}
			}
		}).start();
	}
}
